#include "PreCompile.hpp"
#include "IfDefDirective.hpp"

namespace CPreProc
{
  typedef std::string ParseError;

  IfDefCondition::IfDefCondition(const Glib::ustring &macro_name,bool is_ifndef)
    : macro_name(macro_name),
    is_ifndef(is_ifndef)
  {
  }

  IfDefCondition::IfDefCondition(Tokenizer &tokenizer,int flags)
  {
    is_ifndef = (flags & ParseFlag_IsIfNDef) != 0;

    tokenizer.skip_optional_whitespace();

    Token token = tokenizer.read_pp_token();
    if(token.get_token_type() != TokenType_Identifier)
      throw ParseError("Unexpected token. Macro name expected.");
    macro_name = token.to_string();

    tokenizer.skip_optional_whitespace();

    if(!tokenizer.end_of_data())
      throw ParseError("Unexpected token. End of line expected.");
  }

  Glib::RefPtr<IfDefCondition> IfDefCondition::parse(Tokenizer &tokenizer,int flags)
  {
    return Glib::RefPtr<IfDefCondition>(new IfDefCondition(tokenizer,flags));
  }

  bool IfDefCondition::evaluate(PreProcessorContext &context)
  {
    bool ret = context.is_macro_defined(macro_name);
    if(is_ifndef)
      ret = !ret;
    return ret;
  }

  void IfDefCondition::load(BuildNG::InStream &src)
  {
    assert(!"not implemented yet");
  }

  void IfDefCondition::save(BuildNG::OutStream &dest)
  {
    assert(!"not implemented yet");
  }
}

